package com.lb.jwitter.action.report;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;

import com.lb.jwitter.service.ConditionReport;
import com.lb.jwitter.service.TblQLDTTXAHocKyTrongNam;
import com.lb.jwitter.service.TblQLDTTXAHocKyTrongNamService;
import com.lb.jwitter.service.TblQLDTTXAHocVienTamDungHocKy;
import com.lb.jwitter.service.TblQLDTTXAHocVienTamDungHocKyService;
import com.lb.jwitter.service.TblQLDTTXALopMonHoc;
import com.lb.jwitter.service.TblQLDTTXALopMonHocHocVienService;
import com.lb.jwitter.service.TblQLDTTXALopMonHocService;
import com.lb.jwitter.service.TblQuyenChiTietService;
import com.lb.jwitter.service.TblTaiKhoan;
import com.lb.jwitter.util.ApplicationContextProvider;
import com.lb.jwitter.util.Message;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;

@ContextConfiguration(locations="classpath:beans.xml") 
public class TblQLDTTXALopMonHocReportAction{
    private Message msg; 
    public Message getMsg() { return msg; }

    private Long hocKyId;
    
   public Long getHocKyId() {
		return hocKyId;
	}
	public void setHocKyId(Long hocKyId) {
		this.hocKyId = hocKyId;
	}
   
    private List<TblQLDTTXALopMonHoc> tblQLDTTXALopMonHocs;
    public List<TblQLDTTXALopMonHoc> getTblQLDTTXALopMonHocs() {
		return tblQLDTTXALopMonHocs;
	}
	public void setTblQLDTTXALopMonHocs(
			List<TblQLDTTXALopMonHoc> tblQLDTTXALopMonHocs) {
		this.tblQLDTTXALopMonHocs = tblQLDTTXALopMonHocs;
	}

	private List<Integer> soLuongSVs;
	
	public List<Integer> getSoLuongSVs() {
		return soLuongSVs;
	}
	public void setSoLuongSVs(List<Integer> soLuongSVs) {
		this.soLuongSVs = soLuongSVs;
	}

	private List<TblQLDTTXAHocKyTrongNam> tblQLDTTXAHocKyTrongNams;

	public List<TblQLDTTXAHocKyTrongNam> getTblQLDTTXAHocKyTrongNams() {
		return tblQLDTTXAHocKyTrongNams;
	}

	private String report;
    
    private TblQLDTTXALopMonHocService tblQLDTTXALopMonHocService;
    private TblQLDTTXAHocKyTrongNamService tblQLDTTXAHocKyTrongNamService;
    private TblQLDTTXALopMonHocHocVienService tblQLDTTXALopMonHocHocVienService;

  private TblTaiKhoan taiKhoanDangNhap; 
 public TblTaiKhoan getTaiKhoanDangNhap() { 
     return taiKhoanDangNhap; 
 } 
 public void setTaiKhoanDangNhap(TblTaiKhoan taiKhoanDangNhap) { 
     this.taiKhoanDangNhap = taiKhoanDangNhap; 
 } 
 private TblQuyenChiTietService tblQuyenChiTietService;
	 @Autowired // Injected by Spring 
     ApplicationContextProvider contextProvider; 
 public String form() throws Exception 
{ 
 try{ 	
     Map session = ActionContext.getContext().getSession();
     taiKhoanDangNhap =  (TblTaiKhoan) session.get("taiKhoan");
     if( taiKhoanDangNhap == null) return "noprivilege";
    ApplicationContext context = ApplicationContextProvider.getApplicationContext(); 
    tblQuyenChiTietService = (TblQuyenChiTietService) context.getBean("tbl_Quyen_ChiTietClient"); 
    if(!tblQuyenChiTietService.checkRight(taiKhoanDangNhap.getMaNhomQuyen().getId(), "tbl_QLDTTXA_LopMonHoc", "xem")){ 
        return "noprivilege"; 
    } 
 
    tblQLDTTXAHocKyTrongNamService = (TblQLDTTXAHocKyTrongNamService) context.getBean("tbl_QLDTTXA_HocKyTrongNamClient"); 
        


        msg = new Message(); 
        List<ConditionReport> cons = new ArrayList<ConditionReport>();
        
        { 
            ConditionReport con = new ConditionReport(); 
            con.setCol("id");
            con.setValue1("desc");
            con.setOperator("order");
            cons.add(con);	
        } 
        
       tblQLDTTXAHocKyTrongNams = tblQLDTTXAHocKyTrongNamService.findtblQLDTTXAHocKyTrongNamByCondition(cons, 0, 0);
        
        
        
        return Action.SUCCESS;
	 }catch(Exception e){ 
	     System.out.println("Loi Report tblQLDTTXALopMonHoc Action"); 
	     e.printStackTrace(); 
	     return Action.ERROR; 
	}
}
 
 
 
 
 public String execute() throws Exception 
 { 
  try{ 	
      Map session = ActionContext.getContext().getSession();
      taiKhoanDangNhap =  (TblTaiKhoan) session.get("taiKhoan");
      if( taiKhoanDangNhap == null) return "noprivilege";
     ApplicationContext context = ApplicationContextProvider.getApplicationContext(); 
     tblQuyenChiTietService = (TblQuyenChiTietService) context.getBean("tbl_Quyen_ChiTietClient"); 
     if(!tblQuyenChiTietService.checkRight(taiKhoanDangNhap.getMaNhomQuyen().getId(), "tbl_QLDTTXA_LopMonHoc", "xem")){ 
         return "noprivilege"; 
     } 
     	tblQLDTTXAHocKyTrongNamService = (TblQLDTTXAHocKyTrongNamService) context.getBean("tbl_QLDTTXA_HocKyTrongNamClient");
     	tblQLDTTXALopMonHocService = (TblQLDTTXALopMonHocService)  context.getBean("tbl_QLDTTXA_LopMonHocClient");
     	tblQLDTTXALopMonHocHocVienService = (TblQLDTTXALopMonHocHocVienService) context.getBean("tbl_QLDTTXA_LopMonHoc_HocVienClient");
         
         msg = new Message(); 
         List<ConditionReport> cons = new ArrayList<ConditionReport>();
         
         TblQLDTTXAHocKyTrongNam hocKy = null;
         TblQLDTTXAHocKyTrongNam hocKyTruoc = null;
         
         if(hocKyId != null && !hocKyId.equals(new Long(0))){
        	 hocKy = tblQLDTTXAHocKyTrongNamService.gettblQLDTTXAHocKyTrongNam(hocKyId); 
         }
         
         tblQLDTTXALopMonHocs = new ArrayList<TblQLDTTXALopMonHoc>();
         soLuongSVs = new ArrayList<Integer>();
         
         if(hocKy != null){
        	 cons.clear();
             { 
                 ConditionReport con = new ConditionReport(); 
                 con.setCol("hocKyId.id");
                 con.setValue1(hocKy.getId());
                 con.setOperator("eq");
                 cons.add(con);	
             } 
             
             tblQLDTTXALopMonHocs = tblQLDTTXALopMonHocService.findtblQLDTTXALopMonHocByCondition(cons, 0, 0);
             
         } else {
        	 tblQLDTTXALopMonHocs = new ArrayList<TblQLDTTXALopMonHoc>();
         }
         
         for(TblQLDTTXALopMonHoc lopMonHoc : tblQLDTTXALopMonHocs){
        	 cons.clear();
             { 
                 ConditionReport con = new ConditionReport(); 
                 con.setCol("lopMonHocId.id");
                 con.setValue1(lopMonHoc.getId());
                 con.setOperator("eq");
                 cons.add(con);	
             } 
             int soLuongSV = tblQLDTTXALopMonHocHocVienService.counttblQLDTTXALopMonHocHocVienByCondition(cons);
             soLuongSVs.add(new Integer(soLuongSV));
         }
          
		  
		  return Action.SUCCESS; 
      } 
      catch(Exception e){ 
         System.out.println("Loi Report tblQLDTTXALopMonHoc Action"); 
         e.printStackTrace(); 
         return Action.ERROR; 
 } 
 }

public void setMsg(Message msg) {
	this.msg = msg;
} 




 } 
 